import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import app from './modules/app'

import * as types from './mutation-types'

Vue.use(Vuex)

let loadingTimer = null;
const debug = process.env.NODE_ENV !== 'production'

const state = {
	ajaxing:false,
	loadingZIndex:100,
	direction: 'forward',
	transitionState:true,
	
	deviceType:0,//0 ios //1 android
	isApp:false,//是否是App
	headerHeight:0,//头部高度
	osType:'',
	remLength:'',
}

const getters = {
   
}

const actions = {
    toggleLoading({ commit },data){
		clearTimeout(loadingTimer);
		let flag = data.flag;
		let timeout = data.duration || 10000;
		commit(types.UPDATE_AJAX,flag);
		loadingTimer = setTimeout(()=>{
			if(flag == true){
				commit(types.UPDATE_AJAX,false);
				commit(types.SHOW_MSG,{content:'请求超时,请重试'});
			};
		},timeout);
	},

	updataLoadingZIndex({ commit },zIndex){
		commit(types.UPDATE_ZINDEX,zIndex); 
	},
	setHeaderHeight({ commit },val){
		commit(types.SET_HEADER_HEIGHT,val); 
	},
	setOsType({ commit },val){
		commit(types.SET_OS_TYPE,val); 
	},
	updataIsApp({ commit },val){
		commit(types.ISAPP,val); 
	},
}


const mutations = {
	[types.UPDATE_AJAX] (state, status , zIndex) {
		state.ajaxing= status;
	},
	[types.DEVICE_TYPE] (state, status , zIndex) {
		state.deviceType= status;
	},
	[types.ISAPP] (state, status , zIndex) {
		state.isApp= status;
	},
    [types.UPDATE_DIRECTION] (state, direction) {
      state.direction = direction
    },
    [types.UPDATE_TRANSITION] (state,status) {
      state.transitionState = status
	},

	[types.UPDATE_ZINDEX] (state, zIndex){
		state.loadingZIndex = zIndex;
	},
	[types.SET_HEADER_HEIGHT] (state, val){
		state.headerHeight = val;
		let remLength = window.screen.width / 7.5;
		state.remLength =  remLength;
	},
	[types.SET_OS_TYPE] (state, val){
		state.osType = val;
	},
	[types.ISAPP] (state, val){
		state.finishGetAppId = true;
		state.isApp = val;
	},
}

export default new Vuex.Store({
	state,
	getters,
	actions,
	mutations,
	modules: {
		user,
		app
	},
	strict: debug
})